package com.service.scheduler.job;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.service.alarm.StatisticsAlarm;
import com.service.bean.device.Vehicle;
import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.scheduler.other.AllocateDuty;

public class DutyEndJob implements Job {

	public DutyEndJob() {
		
	}
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			Logger.println("车辆下班定时器启动");
			// 获取JobDetail，并从其中获取放在缓存中的数据
			JobDetail job = context.getJobDetail();
			String gpsNo = job.getJobDataMap().getString("gpsno");
			Vehicle vehicle = CacheManager.instance().getVehicle(gpsNo);
			if (null == vehicle) {
				Logger.debug(gpsNo, "根据GpsNo无法从缓存中获取车辆, 排班统计检查取消");
				return;
			}
			StatisticsAlarm alarm = new StatisticsAlarm();
			alarm.workEndDutyStatistics(vehicle);
			if (null != vehicle.alarmRuleMap && 0 != vehicle.alarmRuleMap.size()) {
				if (vehicle.alarmRuleMap.containsKey("124")) {
					alarm.collectTimeStatistics(vehicle);
				}
				if (vehicle.alarmRuleMap.containsKey("131")) {
					alarm.collectOrderStatistics(vehicle);
				}
				if (vehicle.alarmRuleMap.containsKey("125")) {
					alarm.roadCleaningStatistics(vehicle);
				}
				if (vehicle.alarmRuleMap.containsKey("126")) {
					alarm.workTimesStatistics(vehicle);
				}
			} else {
				Logger.debug(gpsNo, "车辆未绑定报警规则, 不统计");
			}
			// 刷新车辆排班
			CacheManager.instance().refreshDuty(gpsNo);
			// 更新该车辆排班定时器
			AllocateDuty allo = new AllocateDuty();
			allo.updateDuty(vehicle);
		} catch (Exception e) {
			Logger.err(e);
		}
	}

}
